<?php
class ClasseManager
{
  private $_db;

  public function __construct($db)
  {
    $this->setDb($db);
  }

  public function add(Classe $classe)
  {
    $q = $this->_db->prepare('INSERT INTO classe SET nom = :nom, idRefCollege = :idRefCollege');

    $q->bindValue(':nom', $classe->nom());
    $q->bindValue(':idRefCollege', $classe->idRefCollege(), PDO::PARAM_INT);

    $q->execute();
  }

  public function delete(Classe $classe)
  {
    $this->_db->exec('DELETE FROM classe WHERE id = '.$classe->id());
  }

  public function get($id)
  {
    $id = (int) $id;

    $q = $this->_db->query('SELECT * FROM classe WHERE id = '.$id);
    $donnees = $q->fetch();
    return new Classe($donnees);
  }

  public function getList()
  {
    $classes = array();

    $q = $this->_db->query('SELECT * FROM classe ORDER BY id');

    while ($donnees = $q->fetch())
    {
      $classes[] = new Classe($donnees);
    }

    return $classes;
  }

  public function update(Classe $classe)
  {
    $q = $this->_db->prepare('UPDATE classe SET nom = :nom, idRefCollege = :idRefCollege WHERE id = :id');

    $q->bindValue(':nom', $classe->nom());
    $q->bindValue(':idRefCollege', $classe->idRefCollege(), PDO::PARAM_INT);
    $q->bindValue(':id', $classe->id(), PDO::PARAM_INT);

    $q->execute();
  }
  
  public function getByCollege($idRefCollege) //Renvoie toutes les classes d'un collège
  {
    $classes = array();

    $q = $this->_db->query('SELECT * FROM classe WHERE idRefCollege = '.$idRefCollege);

    while ($donnees = $q->fetch())
    {
      $classes[] = new Classe($donnees);
    }

    return $classes;
  }

  public function setDb(PDO $db)
  {
    $this->_db = $db;
  }
}
?>